In [142]:
import numpy as np
import pandas as pd
df=pd.read_csv("C:/Users/sridi/final_mediation.csv")
df.head()
Out[142]:
Arm QOLmental_0 QOLphysical_0 Flourishing_0 Loneliness_0 HealthMotivation_0 Relatedness_0 Anxiety_0 Depression_0 TechComfort_0 ... QOLphysical_3 Flourishing_3 Loneliness_3 HealthMotivation_3 Relatedness_3 Anxiety_3 Depression_3 TechComfort_3 TechPhyLim_3 WellBeing_3
0 1 51.6 42.0 36.0 17 16 32 4.0 4.0 11 ... 40.5 32 17 14 31.0 3 4 10 2 0
1 1 51.3 41.6 34.0 20 12 33 3.0 3.0 12 ... 41.4 30 21 16 30.0 5 4 12 0 1
2 0 54.8 50.4 36.0 8 16 39 0.0 2.0 15 ... 39.6 34 8 18 43.0 0 0 14 0 3
3 1 45.5 46.6 30.0 14 12 29 6.0 1.0 23 ... 42.5 27 19 12 28.0 0 1 28 0 0
4 0 46.6 51.5 40.0 8 18 36 2.0 2.0 6 ... 42.5 35 17 15 36.0 2 5 30 0 2

5 rows × 51 columns

With "WellBeing_3" variable as RV:

In [143]:
from sklearn.linear_model import Lasso

# initiate lasso regression model:
model = Lasso()

# define predictor & response variables:

# define response variable:
y = df['WellBeing_3']

# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
       'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
       'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1',
       'QOLmental_2', 'QOLphysical_2', 'Flourishing_2', 'Loneliness_2',
       'HealthMotivation_2', 'Relatedness_2', 'Anxiety_2', 'Depression_2',
       'TechComfort_2', 'TechPhyLim_2', 'WellBeing_2']]
 # fit regression model:
model.fit(X, y)
Out[143]:
Lasso(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=1000,
      normalize=False, positive=False, precompute=False, random_state=None,
      selection='cyclic', tol=0.0001, warm_start=False)
In [144]:
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['WellBeing_3'])
data_target.shape
data['WellBeing_3'] = pd.Series(data_target)
data
Out[144]:
Arm QOLmental_0 QOLphysical_0 Flourishing_0 Loneliness_0 HealthMotivation_0 Relatedness_0 Anxiety_0 Depression_0 TechComfort_0 ... QOLphysical_3 Flourishing_3 Loneliness_3 HealthMotivation_3 Relatedness_3 Anxiety_3 Depression_3 TechComfort_3 TechPhyLim_3 WellBeing_3
0 1 51.6 42.0 36.0 17 16 32 4.0 4.0 11 ... 40.5 32 17 14 31.0 3 4 10 2 0
1 1 51.3 41.6 34.0 20 12 33 3.0 3.0 12 ... 41.4 30 21 16 30.0 5 4 12 0 1
2 0 54.8 50.4 36.0 8 16 39 0.0 2.0 15 ... 39.6 34 8 18 43.0 0 0 14 0 3
3 1 45.5 46.6 30.0 14 12 29 6.0 1.0 23 ... 42.5 27 19 12 28.0 0 1 28 0 0
4 0 46.6 51.5 40.0 8 18 36 2.0 2.0 6 ... 42.5 35 17 15 36.0 2 5 30 0 2
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
248 1 43.9 50.4 37.0 9 16 43 3.0 3.0 24 ... 50.4 39 10 16 45.0 2 2 21 0 0
249 0 50.9 44.0 33.0 9 19 36 1.0 2.0 14 ... 38.1 31 9 16 33.0 0 2 14 0 0
250 0 45.5 29.7 27.0 19 10 34 2.0 6.0 16 ... 37.5 31 16 12 44.0 0 4 18 2 1
251 0 39.2 42.5 30.0 18 14 22 2.0 2.0 26 ... 40.5 32 16 15 26.0 3 4 5 0 0
252 1 44.7 36.9 33.0 17 9 32 5.0 2.0 2 ... 36.9 31 19 13 29.0 3 5 4 0 0

253 rows × 51 columns

In [145]:
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
       'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
       'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1',
       'QOLmental_2', 'QOLphysical_2', 'Flourishing_2', 'Loneliness_2',
       'HealthMotivation_2', 'Relatedness_2', 'Anxiety_2', 'Depression_2',
       'TechComfort_2', 'TechPhyLim_2', 'WellBeing_2']]
y = data['WellBeing_3']
print(X)
     Arm  QOLmental_0  QOLphysical_0  Flourishing_0  Loneliness_0  \
0      1         51.6           42.0           36.0            17   
1      1         51.3           41.6           34.0            20   
2      0         54.8           50.4           36.0             8   
3      1         45.5           46.6           30.0            14   
4      0         46.6           51.5           40.0             8   
..   ...          ...            ...            ...           ...   
248    1         43.9           50.4           37.0             9   
249    0         50.9           44.0           33.0             9   
250    0         45.5           29.7           27.0            19   
251    0         39.2           42.5           30.0            18   
252    1         44.7           36.9           33.0            17   

     HealthMotivation_0  Relatedness_0  Anxiety_0  Depression_0  \
0                    16             32        4.0           4.0   
1                    12             33        3.0           3.0   
2                    16             39        0.0           2.0   
3                    12             29        6.0           1.0   
4                    18             36        2.0           2.0   
..                  ...            ...        ...           ...   
248                  16             43        3.0           3.0   
249                  19             36        1.0           2.0   
250                  10             34        2.0           6.0   
251                  14             22        2.0           2.0   
252                   9             32        5.0           2.0   

     TechComfort_0  ...  QOLphysical_2  Flourishing_2  Loneliness_2  \
0               11  ...           44.0           32.0          16.0   
1               12  ...           39.6           31.0          14.0   
2               15  ...           46.6           35.0           9.0   
3               23  ...           42.5           28.0          15.0   
4                6  ...           50.4           38.0          12.0   
..             ...  ...            ...            ...           ...   
248             24  ...           31.8           40.0           8.0   
249             14  ...           40.9           32.0           9.0   
250             16  ...           36.5           30.0          14.0   
251             26  ...           44.0           32.0          10.0   
252              2  ...           33.0           31.0          20.0   

     HealthMotivation_2  Relatedness_2  Anxiety_2  Depression_2  \
0                  14.0           36.0        2.0           4.0   
1                  12.0           25.0        5.0           5.0   
2                  19.0           44.0        0.0           4.0   
3                  12.0           29.0        5.0           4.0   
4                  12.0           45.0        0.0           0.0   
..                  ...            ...        ...           ...   
248                15.0           45.0        2.0           4.0   
249                17.0           36.0        4.0           3.0   
250                14.0           43.0        0.0           1.0   
251                13.0           34.0        4.0           2.0   
252                10.0           32.0        4.0           5.0   

     TechComfort_2  TechPhyLim_2  WellBeing_2  
0             15.0           1.0          3.0  
1             12.0           0.0          1.0  
2             14.0           0.0          0.0  
3             23.0           0.0          0.0  
4             30.0           0.0          1.0  
..             ...           ...          ...  
248           26.0           0.0          1.0  
249           14.0           0.0          2.0  
250           16.0           1.0          2.0  
251           29.0           0.0          0.0  
252           11.0           0.0          0.0  

[253 rows x 40 columns]
In [146]:
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, 
                                                    random_state = 1)

print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))

print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
Train data shape of X = (202, 40) and y = (202,) : 
Test data shape of X = (51, 40) and y = (51,) : 

Lasso Regression:

In [147]:
import statsmodels.api as sm
from sklearn.linear_model import Lasso

# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)

# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)

# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")

# Fit regression model:
model = sm.OLS(y, X).fit()

# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")

lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
 
print(lasso_coeff)
Adjusted R-squared: 0.6738336928005866 

               Columns  Coefficient Estimate
0                  Arm             -0.000000
1          QOLmental_0             -0.000000
2        QOLphysical_0             -0.000000
3        Flourishing_0             -0.000000
4         Loneliness_0              0.000000
5   HealthMotivation_0             -0.000000
6        Relatedness_0             -0.000000
7            Anxiety_0              0.000000
8         Depression_0              0.000000
9        TechComfort_0              0.000000
10        TechPhyLim_0              0.000000
11         WellBeing_0              0.000000
12             partner             -0.000000
13          live_alone             -0.000000
14         education_4             -0.000000
15         education_5              0.000000
16         education_6              0.000000
17         education_7              0.000000
18         QOLmental_1             -0.000000
19       QOLphysical_1             -0.000000
20       Flourishing_1             -0.000000
21        Loneliness_1              0.000000
22  HealthMotivation_1             -0.000000
23       Relatedness_1             -0.000000
24           Anxiety_1              0.000000
25        Depression_1              0.000000
26       TechComfort_1             -0.012180
27        TechPhyLim_1              0.000000
28         WellBeing_1              0.000000
29         QOLmental_2             -0.030161
30       QOLphysical_2             -0.005417
31       Flourishing_2             -0.000000
32        Loneliness_2              0.000386
33  HealthMotivation_2              0.000000
34       Relatedness_2             -0.005136
35           Anxiety_2              0.000000
36        Depression_2              0.000000
37       TechComfort_2             -0.000000
38        TechPhyLim_2              0.000000
39         WellBeing_2              0.000000
In [148]:
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(100, 50))
 
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
 
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
 
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))

ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

plt.style.use('ggplot')

# Set general font size:
plt.rcParams['font.size'] = '100'

# Rotate x-axis tick labels:
plt.xticks(rotation = 90)

plt.show()

With "QOLmental_2" variable as RV:

In [149]:
from sklearn.linear_model import Lasso

# initiate lasso regression model:
model = Lasso()

# define predictor & response variables:

# define response variable:
y = df['QOLmental_2']

# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
       'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
       'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1']]
 # fit regression model:
model.fit(X, y)
Out[149]:
Lasso(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=1000,
      normalize=False, positive=False, precompute=False, random_state=None,
      selection='cyclic', tol=0.0001, warm_start=False)
In [150]:
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['QOLmental_2'])
data_target.shape
data['QOLmental_2'] = pd.Series(data_target)
data
Out[150]:
Arm QOLmental_0 QOLphysical_0 Flourishing_0 Loneliness_0 HealthMotivation_0 Relatedness_0 Anxiety_0 Depression_0 TechComfort_0 ... QOLphysical_3 Flourishing_3 Loneliness_3 HealthMotivation_3 Relatedness_3 Anxiety_3 Depression_3 TechComfort_3 TechPhyLim_3 WellBeing_3
0 1 51.6 42.0 36.0 17 16 32 4.0 4.0 11 ... 40.5 32 17 14 31.0 3 4 10 2 0
1 1 51.3 41.6 34.0 20 12 33 3.0 3.0 12 ... 41.4 30 21 16 30.0 5 4 12 0 1
2 0 54.8 50.4 36.0 8 16 39 0.0 2.0 15 ... 39.6 34 8 18 43.0 0 0 14 0 3
3 1 45.5 46.6 30.0 14 12 29 6.0 1.0 23 ... 42.5 27 19 12 28.0 0 1 28 0 0
4 0 46.6 51.5 40.0 8 18 36 2.0 2.0 6 ... 42.5 35 17 15 36.0 2 5 30 0 2
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
248 1 43.9 50.4 37.0 9 16 43 3.0 3.0 24 ... 50.4 39 10 16 45.0 2 2 21 0 0
249 0 50.9 44.0 33.0 9 19 36 1.0 2.0 14 ... 38.1 31 9 16 33.0 0 2 14 0 0
250 0 45.5 29.7 27.0 19 10 34 2.0 6.0 16 ... 37.5 31 16 12 44.0 0 4 18 2 1
251 0 39.2 42.5 30.0 18 14 22 2.0 2.0 26 ... 40.5 32 16 15 26.0 3 4 5 0 0
252 1 44.7 36.9 33.0 17 9 32 5.0 2.0 2 ... 36.9 31 19 13 29.0 3 5 4 0 0

253 rows × 51 columns

In [151]:
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
       'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
       'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1']]
y = data['QOLmental_2']
print(X)
     Arm  QOLmental_0  QOLphysical_0  Flourishing_0  Loneliness_0  \
0      1         51.6           42.0           36.0            17   
1      1         51.3           41.6           34.0            20   
2      0         54.8           50.4           36.0             8   
3      1         45.5           46.6           30.0            14   
4      0         46.6           51.5           40.0             8   
..   ...          ...            ...            ...           ...   
248    1         43.9           50.4           37.0             9   
249    0         50.9           44.0           33.0             9   
250    0         45.5           29.7           27.0            19   
251    0         39.2           42.5           30.0            18   
252    1         44.7           36.9           33.0            17   

     HealthMotivation_0  Relatedness_0  Anxiety_0  Depression_0  \
0                    16             32        4.0           4.0   
1                    12             33        3.0           3.0   
2                    16             39        0.0           2.0   
3                    12             29        6.0           1.0   
4                    18             36        2.0           2.0   
..                  ...            ...        ...           ...   
248                  16             43        3.0           3.0   
249                  19             36        1.0           2.0   
250                  10             34        2.0           6.0   
251                  14             22        2.0           2.0   
252                   9             32        5.0           2.0   

     TechComfort_0  ...  QOLphysical_1  Flourishing_1  Loneliness_1  \
0               11  ...           37.5           36.0          16.0   
1               12  ...           39.6           33.0          17.0   
2               15  ...           44.0           32.0          19.0   
3               23  ...           42.5           28.0          18.0   
4                6  ...           58.6           39.0          12.0   
..             ...  ...            ...            ...           ...   
248             24  ...           53.3           40.0           8.0   
249             14  ...           39.6           31.0          11.0   
250             16  ...           37.5           29.0          20.0   
251             26  ...           41.6           36.0          18.0   
252              2  ...           39.6           30.0          22.0   

     HealthMotivation_1  Relatedness_1  Anxiety_1  Depression_1  \
0                  14.0           27.0        0.0           0.0   
1                  12.0           33.0        5.0           5.0   
2                  14.0           29.0        1.0           6.0   
3                  12.0           26.0        2.0           2.0   
4                  14.0           35.0        2.0           2.0   
..                  ...            ...        ...           ...   
248                18.0           45.0        0.0           0.0   
249                16.0           37.0        2.0           2.0   
250                13.0           31.0        0.0           4.0   
251                12.0           25.0        0.0           5.0   
252                12.0           37.0        3.0           6.0   

     TechComfort_1  TechPhyLim_1  WellBeing_1  
0             11.0           0.0          2.0  
1             14.0           0.0          1.0  
2             14.0           0.0          0.0  
3             20.0           0.0          0.0  
4             30.0           1.0          3.0  
..             ...           ...          ...  
248           26.0           0.0          1.0  
249           14.0           0.0          1.0  
250           20.0           0.0          1.0  
251           29.0           0.0          1.0  
252            5.0           0.0          1.0  

[253 rows x 29 columns]
In [152]:
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, 
                                                    random_state = 1)

print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))

print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
Train data shape of X = (202, 29) and y = (202,) : 
Test data shape of X = (51, 29) and y = (51,) : 

Lasso Regression:

In [153]:
import statsmodels.api as sm
from sklearn.linear_model import Lasso

# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)

# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)

# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")

# Fit regression model:
model = sm.OLS(y, X).fit()

# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")

lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
 
print(lasso_coeff)
Adjusted R-squared: 0.990820142490198 

               Columns  Coefficient Estimate
0                  Arm              0.000000
1          QOLmental_0              0.224187
2        QOLphysical_0              0.012975
3        Flourishing_0              0.043789
4         Loneliness_0             -0.000000
5   HealthMotivation_0              0.000000
6        Relatedness_0             -0.000000
7            Anxiety_0             -0.000000
8         Depression_0             -0.000000
9        TechComfort_0             -0.063117
10        TechPhyLim_0              0.000000
11         WellBeing_0             -0.000000
12             partner             -0.000000
13          live_alone             -0.000000
14         education_4              0.000000
15         education_5             -0.000000
16         education_6              0.000000
17         education_7             -0.000000
18         QOLmental_1              0.397952
19       QOLphysical_1              0.009932
20       Flourishing_1              0.167304
21        Loneliness_1             -0.015032
22  HealthMotivation_1              0.007653
23       Relatedness_1              0.031018
24           Anxiety_1             -0.000000
25        Depression_1             -0.044438
26       TechComfort_1              0.093694
27        TechPhyLim_1             -0.000000
28         WellBeing_1             -0.000000
In [154]:
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(70, 50))
 
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
 
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
 
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))

ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

plt.style.use('ggplot')

# Set general font size:
plt.rcParams['font.size'] = '100'

# Rotate x-axis tick labels:
plt.xticks(rotation = 90)

plt.show()

With "QOLphysical_2" variable as RV:

In [155]:
from sklearn.linear_model import Lasso

# initiate lasso regression model:
model = Lasso()

# define predictor & response variables:

# define response variable:
y = df['QOLphysical_2']

# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
       'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
       'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1']]
 # fit regression model:
model.fit(X, y)
Out[155]:
Lasso(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=1000,
      normalize=False, positive=False, precompute=False, random_state=None,
      selection='cyclic', tol=0.0001, warm_start=False)
In [156]:
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['QOLphysical_2'])
data_target.shape
data['QOLphysical_2'] = pd.Series(data_target)
data
Out[156]:
Arm QOLmental_0 QOLphysical_0 Flourishing_0 Loneliness_0 HealthMotivation_0 Relatedness_0 Anxiety_0 Depression_0 TechComfort_0 ... QOLphysical_3 Flourishing_3 Loneliness_3 HealthMotivation_3 Relatedness_3 Anxiety_3 Depression_3 TechComfort_3 TechPhyLim_3 WellBeing_3
0 1 51.6 42.0 36.0 17 16 32 4.0 4.0 11 ... 40.5 32 17 14 31.0 3 4 10 2 0
1 1 51.3 41.6 34.0 20 12 33 3.0 3.0 12 ... 41.4 30 21 16 30.0 5 4 12 0 1
2 0 54.8 50.4 36.0 8 16 39 0.0 2.0 15 ... 39.6 34 8 18 43.0 0 0 14 0 3
3 1 45.5 46.6 30.0 14 12 29 6.0 1.0 23 ... 42.5 27 19 12 28.0 0 1 28 0 0
4 0 46.6 51.5 40.0 8 18 36 2.0 2.0 6 ... 42.5 35 17 15 36.0 2 5 30 0 2
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
248 1 43.9 50.4 37.0 9 16 43 3.0 3.0 24 ... 50.4 39 10 16 45.0 2 2 21 0 0
249 0 50.9 44.0 33.0 9 19 36 1.0 2.0 14 ... 38.1 31 9 16 33.0 0 2 14 0 0
250 0 45.5 29.7 27.0 19 10 34 2.0 6.0 16 ... 37.5 31 16 12 44.0 0 4 18 2 1
251 0 39.2 42.5 30.0 18 14 22 2.0 2.0 26 ... 40.5 32 16 15 26.0 3 4 5 0 0
252 1 44.7 36.9 33.0 17 9 32 5.0 2.0 2 ... 36.9 31 19 13 29.0 3 5 4 0 0

253 rows × 51 columns

In [157]:
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
       'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
       'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1']]
y = data['QOLphysical_2']
print(X)
     Arm  QOLmental_0  QOLphysical_0  Flourishing_0  Loneliness_0  \
0      1         51.6           42.0           36.0            17   
1      1         51.3           41.6           34.0            20   
2      0         54.8           50.4           36.0             8   
3      1         45.5           46.6           30.0            14   
4      0         46.6           51.5           40.0             8   
..   ...          ...            ...            ...           ...   
248    1         43.9           50.4           37.0             9   
249    0         50.9           44.0           33.0             9   
250    0         45.5           29.7           27.0            19   
251    0         39.2           42.5           30.0            18   
252    1         44.7           36.9           33.0            17   

     HealthMotivation_0  Relatedness_0  Anxiety_0  Depression_0  \
0                    16             32        4.0           4.0   
1                    12             33        3.0           3.0   
2                    16             39        0.0           2.0   
3                    12             29        6.0           1.0   
4                    18             36        2.0           2.0   
..                  ...            ...        ...           ...   
248                  16             43        3.0           3.0   
249                  19             36        1.0           2.0   
250                  10             34        2.0           6.0   
251                  14             22        2.0           2.0   
252                   9             32        5.0           2.0   

     TechComfort_0  ...  QOLphysical_1  Flourishing_1  Loneliness_1  \
0               11  ...           37.5           36.0          16.0   
1               12  ...           39.6           33.0          17.0   
2               15  ...           44.0           32.0          19.0   
3               23  ...           42.5           28.0          18.0   
4                6  ...           58.6           39.0          12.0   
..             ...  ...            ...            ...           ...   
248             24  ...           53.3           40.0           8.0   
249             14  ...           39.6           31.0          11.0   
250             16  ...           37.5           29.0          20.0   
251             26  ...           41.6           36.0          18.0   
252              2  ...           39.6           30.0          22.0   

     HealthMotivation_1  Relatedness_1  Anxiety_1  Depression_1  \
0                  14.0           27.0        0.0           0.0   
1                  12.0           33.0        5.0           5.0   
2                  14.0           29.0        1.0           6.0   
3                  12.0           26.0        2.0           2.0   
4                  14.0           35.0        2.0           2.0   
..                  ...            ...        ...           ...   
248                18.0           45.0        0.0           0.0   
249                16.0           37.0        2.0           2.0   
250                13.0           31.0        0.0           4.0   
251                12.0           25.0        0.0           5.0   
252                12.0           37.0        3.0           6.0   

     TechComfort_1  TechPhyLim_1  WellBeing_1  
0             11.0           0.0          2.0  
1             14.0           0.0          1.0  
2             14.0           0.0          0.0  
3             20.0           0.0          0.0  
4             30.0           1.0          3.0  
..             ...           ...          ...  
248           26.0           0.0          1.0  
249           14.0           0.0          1.0  
250           20.0           0.0          1.0  
251           29.0           0.0          1.0  
252            5.0           0.0          1.0  

[253 rows x 29 columns]
In [158]:
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, 
                                                    random_state = 1)

print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))

print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
Train data shape of X = (202, 29) and y = (202,) : 
Test data shape of X = (51, 29) and y = (51,) : 

Lasso Regression:

In [159]:
import statsmodels.api as sm
from sklearn.linear_model import Lasso

# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)

# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)

# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")

# Fit regression model:
model = sm.OLS(y, X).fit()

# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")

lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
 
print(lasso_coeff)
Adjusted R-squared: 0.9906849763526787 

               Columns  Coefficient Estimate
0                  Arm              0.000000
1          QOLmental_0              0.108523
2        QOLphysical_0              0.331946
3        Flourishing_0             -0.043104
4         Loneliness_0              0.048034
5   HealthMotivation_0             -0.000000
6        Relatedness_0             -0.000000
7            Anxiety_0             -0.181878
8         Depression_0             -0.081077
9        TechComfort_0             -0.000000
10        TechPhyLim_0             -0.000000
11         WellBeing_0             -0.000000
12             partner             -0.000000
13          live_alone              0.000000
14         education_4              0.000000
15         education_5             -0.000000
16         education_6             -0.000000
17         education_7              0.000000
18         QOLmental_1              0.031757
19       QOLphysical_1              0.277347
20       Flourishing_1              0.003633
21        Loneliness_1             -0.054310
22  HealthMotivation_1              0.000000
23       Relatedness_1              0.024222
24           Anxiety_1             -0.000000
25        Depression_1             -0.000000
26       TechComfort_1             -0.013449
27        TechPhyLim_1              0.000000
28         WellBeing_1             -0.000000
In [160]:
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(70, 50))
 
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
 
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
 
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))

ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

plt.style.use('ggplot')

# Set general font size:
plt.rcParams['font.size'] = '100'

# Rotate x-axis tick labels:
plt.xticks(rotation = 90)

plt.show()

With "Loneliness_2" variable as RV:

In [161]:
from sklearn.linear_model import Lasso

# initiate lasso regression model:
model = Lasso()

# define predictor & response variables:

# define response variable:
y = df['Loneliness_2']

# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
       'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
       'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1']]
 # fit regression model:
model.fit(X, y)
Out[161]:
Lasso(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=1000,
      normalize=False, positive=False, precompute=False, random_state=None,
      selection='cyclic', tol=0.0001, warm_start=False)
In [162]:
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['Loneliness_2'])
data_target.shape
data['Loneliness_2'] = pd.Series(data_target)
data
Out[162]:
Arm QOLmental_0 QOLphysical_0 Flourishing_0 Loneliness_0 HealthMotivation_0 Relatedness_0 Anxiety_0 Depression_0 TechComfort_0 ... QOLphysical_3 Flourishing_3 Loneliness_3 HealthMotivation_3 Relatedness_3 Anxiety_3 Depression_3 TechComfort_3 TechPhyLim_3 WellBeing_3
0 1 51.6 42.0 36.0 17 16 32 4.0 4.0 11 ... 40.5 32 17 14 31.0 3 4 10 2 0
1 1 51.3 41.6 34.0 20 12 33 3.0 3.0 12 ... 41.4 30 21 16 30.0 5 4 12 0 1
2 0 54.8 50.4 36.0 8 16 39 0.0 2.0 15 ... 39.6 34 8 18 43.0 0 0 14 0 3
3 1 45.5 46.6 30.0 14 12 29 6.0 1.0 23 ... 42.5 27 19 12 28.0 0 1 28 0 0
4 0 46.6 51.5 40.0 8 18 36 2.0 2.0 6 ... 42.5 35 17 15 36.0 2 5 30 0 2
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
248 1 43.9 50.4 37.0 9 16 43 3.0 3.0 24 ... 50.4 39 10 16 45.0 2 2 21 0 0
249 0 50.9 44.0 33.0 9 19 36 1.0 2.0 14 ... 38.1 31 9 16 33.0 0 2 14 0 0
250 0 45.5 29.7 27.0 19 10 34 2.0 6.0 16 ... 37.5 31 16 12 44.0 0 4 18 2 1
251 0 39.2 42.5 30.0 18 14 22 2.0 2.0 26 ... 40.5 32 16 15 26.0 3 4 5 0 0
252 1 44.7 36.9 33.0 17 9 32 5.0 2.0 2 ... 36.9 31 19 13 29.0 3 5 4 0 0

253 rows × 51 columns

In [163]:
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
       'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
       'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1']]
y = data['Loneliness_2']
print(X)
     Arm  QOLmental_0  QOLphysical_0  Flourishing_0  Loneliness_0  \
0      1         51.6           42.0           36.0            17   
1      1         51.3           41.6           34.0            20   
2      0         54.8           50.4           36.0             8   
3      1         45.5           46.6           30.0            14   
4      0         46.6           51.5           40.0             8   
..   ...          ...            ...            ...           ...   
248    1         43.9           50.4           37.0             9   
249    0         50.9           44.0           33.0             9   
250    0         45.5           29.7           27.0            19   
251    0         39.2           42.5           30.0            18   
252    1         44.7           36.9           33.0            17   

     HealthMotivation_0  Relatedness_0  Anxiety_0  Depression_0  \
0                    16             32        4.0           4.0   
1                    12             33        3.0           3.0   
2                    16             39        0.0           2.0   
3                    12             29        6.0           1.0   
4                    18             36        2.0           2.0   
..                  ...            ...        ...           ...   
248                  16             43        3.0           3.0   
249                  19             36        1.0           2.0   
250                  10             34        2.0           6.0   
251                  14             22        2.0           2.0   
252                   9             32        5.0           2.0   

     TechComfort_0  ...  QOLphysical_1  Flourishing_1  Loneliness_1  \
0               11  ...           37.5           36.0          16.0   
1               12  ...           39.6           33.0          17.0   
2               15  ...           44.0           32.0          19.0   
3               23  ...           42.5           28.0          18.0   
4                6  ...           58.6           39.0          12.0   
..             ...  ...            ...            ...           ...   
248             24  ...           53.3           40.0           8.0   
249             14  ...           39.6           31.0          11.0   
250             16  ...           37.5           29.0          20.0   
251             26  ...           41.6           36.0          18.0   
252              2  ...           39.6           30.0          22.0   

     HealthMotivation_1  Relatedness_1  Anxiety_1  Depression_1  \
0                  14.0           27.0        0.0           0.0   
1                  12.0           33.0        5.0           5.0   
2                  14.0           29.0        1.0           6.0   
3                  12.0           26.0        2.0           2.0   
4                  14.0           35.0        2.0           2.0   
..                  ...            ...        ...           ...   
248                18.0           45.0        0.0           0.0   
249                16.0           37.0        2.0           2.0   
250                13.0           31.0        0.0           4.0   
251                12.0           25.0        0.0           5.0   
252                12.0           37.0        3.0           6.0   

     TechComfort_1  TechPhyLim_1  WellBeing_1  
0             11.0           0.0          2.0  
1             14.0           0.0          1.0  
2             14.0           0.0          0.0  
3             20.0           0.0          0.0  
4             30.0           1.0          3.0  
..             ...           ...          ...  
248           26.0           0.0          1.0  
249           14.0           0.0          1.0  
250           20.0           0.0          1.0  
251           29.0           0.0          1.0  
252            5.0           0.0          1.0  

[253 rows x 29 columns]
In [164]:
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, 
                                                    random_state = 1)

print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))

print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
Train data shape of X = (202, 29) and y = (202,) : 
Test data shape of X = (51, 29) and y = (51,) : 

Lasso Regression:

In [165]:
import statsmodels.api as sm
from sklearn.linear_model import Lasso

# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)

# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)

# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")

# Fit regression model:
model = sm.OLS(y, X).fit()

# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")

lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
 
print(lasso_coeff)
Adjusted R-squared: 0.9629209352703829 

               Columns  Coefficient Estimate
0                  Arm              0.000000
1          QOLmental_0              0.000000
2        QOLphysical_0              0.055993
3        Flourishing_0             -0.000000
4         Loneliness_0              0.335050
5   HealthMotivation_0             -0.000000
6        Relatedness_0             -0.000000
7            Anxiety_0              0.000000
8         Depression_0              0.000000
9        TechComfort_0              0.008602
10        TechPhyLim_0             -0.000000
11         WellBeing_0              0.000000
12             partner              0.000000
13          live_alone             -0.000000
14         education_4              0.000000
15         education_5              0.000000
16         education_6             -0.000000
17         education_7              0.000000
18         QOLmental_1             -0.048220
19       QOLphysical_1              0.002431
20       Flourishing_1             -0.014561
21        Loneliness_1              0.411493
22  HealthMotivation_1              0.000000
23       Relatedness_1             -0.091919
24           Anxiety_1              0.000000
25        Depression_1              0.000000
26       TechComfort_1              0.000000
27        TechPhyLim_1             -0.000000
28         WellBeing_1              0.000000
In [166]:
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(70, 50))
 
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
 
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
 
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))

ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

plt.style.use('ggplot')

# Set general font size:
plt.rcParams['font.size'] = '100'

# Rotate x-axis tick labels:
plt.xticks(rotation = 90)

plt.show()

With "Relatedness_2" variable as RV:

In [167]:
from sklearn.linear_model import Lasso

# initiate lasso regression model:
model = Lasso()

# define predictor & response variables:

# define response variable:
y = df['Relatedness_2']

# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
       'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
       'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1']]
 # fit regression model:
model.fit(X, y)
Out[167]:
Lasso(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=1000,
      normalize=False, positive=False, precompute=False, random_state=None,
      selection='cyclic', tol=0.0001, warm_start=False)
In [168]:
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['Relatedness_2'])
data_target.shape
data['Relatedness_2'] = pd.Series(data_target)
data
Out[168]:
Arm QOLmental_0 QOLphysical_0 Flourishing_0 Loneliness_0 HealthMotivation_0 Relatedness_0 Anxiety_0 Depression_0 TechComfort_0 ... QOLphysical_3 Flourishing_3 Loneliness_3 HealthMotivation_3 Relatedness_3 Anxiety_3 Depression_3 TechComfort_3 TechPhyLim_3 WellBeing_3
0 1 51.6 42.0 36.0 17 16 32 4.0 4.0 11 ... 40.5 32 17 14 31.0 3 4 10 2 0
1 1 51.3 41.6 34.0 20 12 33 3.0 3.0 12 ... 41.4 30 21 16 30.0 5 4 12 0 1
2 0 54.8 50.4 36.0 8 16 39 0.0 2.0 15 ... 39.6 34 8 18 43.0 0 0 14 0 3
3 1 45.5 46.6 30.0 14 12 29 6.0 1.0 23 ... 42.5 27 19 12 28.0 0 1 28 0 0
4 0 46.6 51.5 40.0 8 18 36 2.0 2.0 6 ... 42.5 35 17 15 36.0 2 5 30 0 2
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
248 1 43.9 50.4 37.0 9 16 43 3.0 3.0 24 ... 50.4 39 10 16 45.0 2 2 21 0 0
249 0 50.9 44.0 33.0 9 19 36 1.0 2.0 14 ... 38.1 31 9 16 33.0 0 2 14 0 0
250 0 45.5 29.7 27.0 19 10 34 2.0 6.0 16 ... 37.5 31 16 12 44.0 0 4 18 2 1
251 0 39.2 42.5 30.0 18 14 22 2.0 2.0 26 ... 40.5 32 16 15 26.0 3 4 5 0 0
252 1 44.7 36.9 33.0 17 9 32 5.0 2.0 2 ... 36.9 31 19 13 29.0 3 5 4 0 0

253 rows × 51 columns

In [169]:
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
       'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
       'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1']]
y = data['Relatedness_2']
print(X)
     Arm  QOLmental_0  QOLphysical_0  Flourishing_0  Loneliness_0  \
0      1         51.6           42.0           36.0            17   
1      1         51.3           41.6           34.0            20   
2      0         54.8           50.4           36.0             8   
3      1         45.5           46.6           30.0            14   
4      0         46.6           51.5           40.0             8   
..   ...          ...            ...            ...           ...   
248    1         43.9           50.4           37.0             9   
249    0         50.9           44.0           33.0             9   
250    0         45.5           29.7           27.0            19   
251    0         39.2           42.5           30.0            18   
252    1         44.7           36.9           33.0            17   

     HealthMotivation_0  Relatedness_0  Anxiety_0  Depression_0  \
0                    16             32        4.0           4.0   
1                    12             33        3.0           3.0   
2                    16             39        0.0           2.0   
3                    12             29        6.0           1.0   
4                    18             36        2.0           2.0   
..                  ...            ...        ...           ...   
248                  16             43        3.0           3.0   
249                  19             36        1.0           2.0   
250                  10             34        2.0           6.0   
251                  14             22        2.0           2.0   
252                   9             32        5.0           2.0   

     TechComfort_0  ...  QOLphysical_1  Flourishing_1  Loneliness_1  \
0               11  ...           37.5           36.0          16.0   
1               12  ...           39.6           33.0          17.0   
2               15  ...           44.0           32.0          19.0   
3               23  ...           42.5           28.0          18.0   
4                6  ...           58.6           39.0          12.0   
..             ...  ...            ...            ...           ...   
248             24  ...           53.3           40.0           8.0   
249             14  ...           39.6           31.0          11.0   
250             16  ...           37.5           29.0          20.0   
251             26  ...           41.6           36.0          18.0   
252              2  ...           39.6           30.0          22.0   

     HealthMotivation_1  Relatedness_1  Anxiety_1  Depression_1  \
0                  14.0           27.0        0.0           0.0   
1                  12.0           33.0        5.0           5.0   
2                  14.0           29.0        1.0           6.0   
3                  12.0           26.0        2.0           2.0   
4                  14.0           35.0        2.0           2.0   
..                  ...            ...        ...           ...   
248                18.0           45.0        0.0           0.0   
249                16.0           37.0        2.0           2.0   
250                13.0           31.0        0.0           4.0   
251                12.0           25.0        0.0           5.0   
252                12.0           37.0        3.0           6.0   

     TechComfort_1  TechPhyLim_1  WellBeing_1  
0             11.0           0.0          2.0  
1             14.0           0.0          1.0  
2             14.0           0.0          0.0  
3             20.0           0.0          0.0  
4             30.0           1.0          3.0  
..             ...           ...          ...  
248           26.0           0.0          1.0  
249           14.0           0.0          1.0  
250           20.0           0.0          1.0  
251           29.0           0.0          1.0  
252            5.0           0.0          1.0  

[253 rows x 29 columns]
In [170]:
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, 
                                                    random_state = 1)

print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))

print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
Train data shape of X = (202, 29) and y = (202,) : 
Test data shape of X = (51, 29) and y = (51,) : 

Lasso Regression:

In [171]:
import statsmodels.api as sm
from sklearn.linear_model import Lasso

# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)

# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)

# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")

# Fit regression model:
model = sm.OLS(y, X).fit()

# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")

lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
 
print(lasso_coeff)
Adjusted R-squared: 0.9827325032371167 

               Columns  Coefficient Estimate
0                  Arm             -0.000000
1          QOLmental_0              0.011923
2        QOLphysical_0              0.000000
3        Flourishing_0              0.000000
4         Loneliness_0             -0.142062
5   HealthMotivation_0              0.143028
6        Relatedness_0              0.211779
7            Anxiety_0              0.000000
8         Depression_0             -0.000000
9        TechComfort_0             -0.000000
10        TechPhyLim_0              0.000000
11         WellBeing_0              0.000000
12             partner             -0.000000
13          live_alone              0.000000
14         education_4             -0.000000
15         education_5              0.000000
16         education_6             -0.000000
17         education_7             -0.000000
18         QOLmental_1              0.000000
19       QOLphysical_1              0.000000
20       Flourishing_1              0.000000
21        Loneliness_1             -0.000000
22  HealthMotivation_1             -0.000000
23       Relatedness_1              0.595815
24           Anxiety_1             -0.000000
25        Depression_1             -0.022999
26       TechComfort_1              0.000000
27        TechPhyLim_1              0.000000
28         WellBeing_1             -0.000000
In [172]:
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(70, 50))
 
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
 
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
 
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))

ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

plt.style.use('ggplot')

# Set general font size:
plt.rcParams['font.size'] = '100'

# Rotate x-axis tick labels:
plt.xticks(rotation = 90)

plt.show()

With "Flourishing_1" variable as RV:

In [173]:
from sklearn.linear_model import Lasso

# initiate lasso regression model:
model = Lasso()

# define predictor & response variables:

# define response variable:
y = df['Flourishing_1']

# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7']]
 # fit regression model:
model.fit(X, y)
Out[173]:
Lasso(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=1000,
      normalize=False, positive=False, precompute=False, random_state=None,
      selection='cyclic', tol=0.0001, warm_start=False)
In [174]:
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['Flourishing_1'])
data_target.shape
data['Flourishing_1'] = pd.Series(data_target)
data
Out[174]:
Arm QOLmental_0 QOLphysical_0 Flourishing_0 Loneliness_0 HealthMotivation_0 Relatedness_0 Anxiety_0 Depression_0 TechComfort_0 ... QOLphysical_3 Flourishing_3 Loneliness_3 HealthMotivation_3 Relatedness_3 Anxiety_3 Depression_3 TechComfort_3 TechPhyLim_3 WellBeing_3
0 1 51.6 42.0 36.0 17 16 32 4.0 4.0 11 ... 40.5 32 17 14 31.0 3 4 10 2 0
1 1 51.3 41.6 34.0 20 12 33 3.0 3.0 12 ... 41.4 30 21 16 30.0 5 4 12 0 1
2 0 54.8 50.4 36.0 8 16 39 0.0 2.0 15 ... 39.6 34 8 18 43.0 0 0 14 0 3
3 1 45.5 46.6 30.0 14 12 29 6.0 1.0 23 ... 42.5 27 19 12 28.0 0 1 28 0 0
4 0 46.6 51.5 40.0 8 18 36 2.0 2.0 6 ... 42.5 35 17 15 36.0 2 5 30 0 2
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
248 1 43.9 50.4 37.0 9 16 43 3.0 3.0 24 ... 50.4 39 10 16 45.0 2 2 21 0 0
249 0 50.9 44.0 33.0 9 19 36 1.0 2.0 14 ... 38.1 31 9 16 33.0 0 2 14 0 0
250 0 45.5 29.7 27.0 19 10 34 2.0 6.0 16 ... 37.5 31 16 12 44.0 0 4 18 2 1
251 0 39.2 42.5 30.0 18 14 22 2.0 2.0 26 ... 40.5 32 16 15 26.0 3 4 5 0 0
252 1 44.7 36.9 33.0 17 9 32 5.0 2.0 2 ... 36.9 31 19 13 29.0 3 5 4 0 0

253 rows × 51 columns

In [175]:
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7']]
y = data['Flourishing_1']
print(X)
     Arm  QOLmental_0  QOLphysical_0  Flourishing_0  Loneliness_0  \
0      1         51.6           42.0           36.0            17   
1      1         51.3           41.6           34.0            20   
2      0         54.8           50.4           36.0             8   
3      1         45.5           46.6           30.0            14   
4      0         46.6           51.5           40.0             8   
..   ...          ...            ...            ...           ...   
248    1         43.9           50.4           37.0             9   
249    0         50.9           44.0           33.0             9   
250    0         45.5           29.7           27.0            19   
251    0         39.2           42.5           30.0            18   
252    1         44.7           36.9           33.0            17   

     HealthMotivation_0  Relatedness_0  Anxiety_0  Depression_0  \
0                    16             32        4.0           4.0   
1                    12             33        3.0           3.0   
2                    16             39        0.0           2.0   
3                    12             29        6.0           1.0   
4                    18             36        2.0           2.0   
..                  ...            ...        ...           ...   
248                  16             43        3.0           3.0   
249                  19             36        1.0           2.0   
250                  10             34        2.0           6.0   
251                  14             22        2.0           2.0   
252                   9             32        5.0           2.0   

     TechComfort_0  TechPhyLim_0  WellBeing_0  partner  live_alone  \
0               11             2            0        0           0   
1               12             0            0        1           1   
2               15             0            0        1           1   
3               23             0            0        1           1   
4                6             2            0        0           1   
..             ...           ...          ...      ...         ...   
248             24             0            0        1           1   
249             14             1            1        0           1   
250             16             1            1        0           0   
251             26             0            0        0           0   
252              2             0            0        0           0   

     education_4  education_5  education_6  education_7  
0              0            0            0            1  
1              0            0            0            1  
2              0            0            1            0  
3              0            1            0            0  
4              0            0            1            0  
..           ...          ...          ...          ...  
248            0            0            1            0  
249            0            0            0            0  
250            0            0            0            0  
251            0            1            0            0  
252            0            0            0            0  

[253 rows x 18 columns]
In [176]:
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, 
                                                    random_state = 1)

print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))

print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
Train data shape of X = (202, 18) and y = (202,) : 
Test data shape of X = (51, 18) and y = (51,) : 

Lasso Regression:

In [177]:
import statsmodels.api as sm
from sklearn.linear_model import Lasso

# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)

# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)

# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")

# Fit regression model:
model = sm.OLS(y, X).fit()

# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")

lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
 
print(lasso_coeff)
Adjusted R-squared: 0.9831015508881848 

               Columns  Coefficient Estimate
0                  Arm              0.000000
1          QOLmental_0              0.158245
2        QOLphysical_0              0.000848
3        Flourishing_0              0.249090
4         Loneliness_0             -0.008114
5   HealthMotivation_0             -0.000000
6        Relatedness_0              0.116663
7            Anxiety_0              0.000000
8         Depression_0             -0.096113
9        TechComfort_0              0.032193
10        TechPhyLim_0              0.000000
11         WellBeing_0              0.000000
12             partner             -0.000000
13          live_alone             -0.000000
14         education_4             -0.000000
15         education_5              0.000000
16         education_6              0.000000
17         education_7              0.000000
In [178]:
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(100, 50))
 
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
 
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
 
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))

ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

plt.style.use('ggplot')

# Set general font size:
plt.rcParams['font.size'] = '100'

# Rotate x-axis tick labels:
plt.xticks(rotation = 90)

plt.show()

With "QOLmental_1" variable as RV:

In [179]:
from sklearn.linear_model import Lasso

# initiate lasso regression model:
model = Lasso()

# define predictor & response variables:

# define response variable:
y = df['QOLmental_1']

# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7']]
 # fit regression model:
model.fit(X, y)
Out[179]:
Lasso(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=1000,
      normalize=False, positive=False, precompute=False, random_state=None,
      selection='cyclic', tol=0.0001, warm_start=False)
In [180]:
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['QOLmental_1'])
data_target.shape
data['QOLmental_1'] = pd.Series(data_target)
data
Out[180]:
Arm QOLmental_0 QOLphysical_0 Flourishing_0 Loneliness_0 HealthMotivation_0 Relatedness_0 Anxiety_0 Depression_0 TechComfort_0 ... QOLphysical_3 Flourishing_3 Loneliness_3 HealthMotivation_3 Relatedness_3 Anxiety_3 Depression_3 TechComfort_3 TechPhyLim_3 WellBeing_3
0 1 51.6 42.0 36.0 17 16 32 4.0 4.0 11 ... 40.5 32 17 14 31.0 3 4 10 2 0
1 1 51.3 41.6 34.0 20 12 33 3.0 3.0 12 ... 41.4 30 21 16 30.0 5 4 12 0 1
2 0 54.8 50.4 36.0 8 16 39 0.0 2.0 15 ... 39.6 34 8 18 43.0 0 0 14 0 3
3 1 45.5 46.6 30.0 14 12 29 6.0 1.0 23 ... 42.5 27 19 12 28.0 0 1 28 0 0
4 0 46.6 51.5 40.0 8 18 36 2.0 2.0 6 ... 42.5 35 17 15 36.0 2 5 30 0 2
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
248 1 43.9 50.4 37.0 9 16 43 3.0 3.0 24 ... 50.4 39 10 16 45.0 2 2 21 0 0
249 0 50.9 44.0 33.0 9 19 36 1.0 2.0 14 ... 38.1 31 9 16 33.0 0 2 14 0 0
250 0 45.5 29.7 27.0 19 10 34 2.0 6.0 16 ... 37.5 31 16 12 44.0 0 4 18 2 1
251 0 39.2 42.5 30.0 18 14 22 2.0 2.0 26 ... 40.5 32 16 15 26.0 3 4 5 0 0
252 1 44.7 36.9 33.0 17 9 32 5.0 2.0 2 ... 36.9 31 19 13 29.0 3 5 4 0 0

253 rows × 51 columns

In [181]:
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7']]
y = data['QOLmental_1']
print(X)
     Arm  QOLmental_0  QOLphysical_0  Flourishing_0  Loneliness_0  \
0      1         51.6           42.0           36.0            17   
1      1         51.3           41.6           34.0            20   
2      0         54.8           50.4           36.0             8   
3      1         45.5           46.6           30.0            14   
4      0         46.6           51.5           40.0             8   
..   ...          ...            ...            ...           ...   
248    1         43.9           50.4           37.0             9   
249    0         50.9           44.0           33.0             9   
250    0         45.5           29.7           27.0            19   
251    0         39.2           42.5           30.0            18   
252    1         44.7           36.9           33.0            17   

     HealthMotivation_0  Relatedness_0  Anxiety_0  Depression_0  \
0                    16             32        4.0           4.0   
1                    12             33        3.0           3.0   
2                    16             39        0.0           2.0   
3                    12             29        6.0           1.0   
4                    18             36        2.0           2.0   
..                  ...            ...        ...           ...   
248                  16             43        3.0           3.0   
249                  19             36        1.0           2.0   
250                  10             34        2.0           6.0   
251                  14             22        2.0           2.0   
252                   9             32        5.0           2.0   

     TechComfort_0  TechPhyLim_0  WellBeing_0  partner  live_alone  \
0               11             2            0        0           0   
1               12             0            0        1           1   
2               15             0            0        1           1   
3               23             0            0        1           1   
4                6             2            0        0           1   
..             ...           ...          ...      ...         ...   
248             24             0            0        1           1   
249             14             1            1        0           1   
250             16             1            1        0           0   
251             26             0            0        0           0   
252              2             0            0        0           0   

     education_4  education_5  education_6  education_7  
0              0            0            0            1  
1              0            0            0            1  
2              0            0            1            0  
3              0            1            0            0  
4              0            0            1            0  
..           ...          ...          ...          ...  
248            0            0            1            0  
249            0            0            0            0  
250            0            0            0            0  
251            0            1            0            0  
252            0            0            0            0  

[253 rows x 18 columns]
In [182]:
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, 
                                                    random_state = 1)

print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))

print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
Train data shape of X = (202, 18) and y = (202,) : 
Test data shape of X = (51, 18) and y = (51,) : 

Lasso Regression:

In [183]:
import statsmodels.api as sm
from sklearn.linear_model import Lasso

# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)

# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)

# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")

# Fit regression model:
model = sm.OLS(y, X).fit()

# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")

lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
 
print(lasso_coeff)
Adjusted R-squared: 0.9898785041811424 

               Columns  Coefficient Estimate
0                  Arm              0.000000
1          QOLmental_0              0.574617
2        QOLphysical_0              0.129635
3        Flourishing_0              0.000000
4         Loneliness_0             -0.000000
5   HealthMotivation_0              0.000000
6        Relatedness_0              0.156820
7            Anxiety_0             -0.240566
8         Depression_0              0.000000
9        TechComfort_0              0.071050
10        TechPhyLim_0             -0.000000
11         WellBeing_0             -0.000000
12             partner             -0.000000
13          live_alone              0.000000
14         education_4              0.000000
15         education_5              0.000000
16         education_6              0.000000
17         education_7             -0.000000
In [184]:
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(70, 50))
 
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
 
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
 
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))

ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

plt.style.use('ggplot')

# Set general font size:
plt.rcParams['font.size'] = '100'

# Rotate x-axis tick labels:
plt.xticks(rotation = 90)

plt.show()

With "QOLphysical_1" variable as RV:

In [185]:
from sklearn.linear_model import Lasso

# initiate lasso regression model:
model = Lasso()

# define predictor & response variables:

# define response variable:
y = df['QOLphysical_1']

# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7']]
 # fit regression model:
model.fit(X, y)
Out[185]:
Lasso(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=1000,
      normalize=False, positive=False, precompute=False, random_state=None,
      selection='cyclic', tol=0.0001, warm_start=False)
In [186]:
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['QOLphysical_1'])
data_target.shape
data['QOLphysical_1'] = pd.Series(data_target)
data
Out[186]:
Arm QOLmental_0 QOLphysical_0 Flourishing_0 Loneliness_0 HealthMotivation_0 Relatedness_0 Anxiety_0 Depression_0 TechComfort_0 ... QOLphysical_3 Flourishing_3 Loneliness_3 HealthMotivation_3 Relatedness_3 Anxiety_3 Depression_3 TechComfort_3 TechPhyLim_3 WellBeing_3
0 1 51.6 42.0 36.0 17 16 32 4.0 4.0 11 ... 40.5 32 17 14 31.0 3 4 10 2 0
1 1 51.3 41.6 34.0 20 12 33 3.0 3.0 12 ... 41.4 30 21 16 30.0 5 4 12 0 1
2 0 54.8 50.4 36.0 8 16 39 0.0 2.0 15 ... 39.6 34 8 18 43.0 0 0 14 0 3
3 1 45.5 46.6 30.0 14 12 29 6.0 1.0 23 ... 42.5 27 19 12 28.0 0 1 28 0 0
4 0 46.6 51.5 40.0 8 18 36 2.0 2.0 6 ... 42.5 35 17 15 36.0 2 5 30 0 2
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
248 1 43.9 50.4 37.0 9 16 43 3.0 3.0 24 ... 50.4 39 10 16 45.0 2 2 21 0 0
249 0 50.9 44.0 33.0 9 19 36 1.0 2.0 14 ... 38.1 31 9 16 33.0 0 2 14 0 0
250 0 45.5 29.7 27.0 19 10 34 2.0 6.0 16 ... 37.5 31 16 12 44.0 0 4 18 2 1
251 0 39.2 42.5 30.0 18 14 22 2.0 2.0 26 ... 40.5 32 16 15 26.0 3 4 5 0 0
252 1 44.7 36.9 33.0 17 9 32 5.0 2.0 2 ... 36.9 31 19 13 29.0 3 5 4 0 0

253 rows × 51 columns

In [187]:
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7']]
y = data['QOLphysical_1']
print(X)
     Arm  QOLmental_0  QOLphysical_0  Flourishing_0  Loneliness_0  \
0      1         51.6           42.0           36.0            17   
1      1         51.3           41.6           34.0            20   
2      0         54.8           50.4           36.0             8   
3      1         45.5           46.6           30.0            14   
4      0         46.6           51.5           40.0             8   
..   ...          ...            ...            ...           ...   
248    1         43.9           50.4           37.0             9   
249    0         50.9           44.0           33.0             9   
250    0         45.5           29.7           27.0            19   
251    0         39.2           42.5           30.0            18   
252    1         44.7           36.9           33.0            17   

     HealthMotivation_0  Relatedness_0  Anxiety_0  Depression_0  \
0                    16             32        4.0           4.0   
1                    12             33        3.0           3.0   
2                    16             39        0.0           2.0   
3                    12             29        6.0           1.0   
4                    18             36        2.0           2.0   
..                  ...            ...        ...           ...   
248                  16             43        3.0           3.0   
249                  19             36        1.0           2.0   
250                  10             34        2.0           6.0   
251                  14             22        2.0           2.0   
252                   9             32        5.0           2.0   

     TechComfort_0  TechPhyLim_0  WellBeing_0  partner  live_alone  \
0               11             2            0        0           0   
1               12             0            0        1           1   
2               15             0            0        1           1   
3               23             0            0        1           1   
4                6             2            0        0           1   
..             ...           ...          ...      ...         ...   
248             24             0            0        1           1   
249             14             1            1        0           1   
250             16             1            1        0           0   
251             26             0            0        0           0   
252              2             0            0        0           0   

     education_4  education_5  education_6  education_7  
0              0            0            0            1  
1              0            0            0            1  
2              0            0            1            0  
3              0            1            0            0  
4              0            0            1            0  
..           ...          ...          ...          ...  
248            0            0            1            0  
249            0            0            0            0  
250            0            0            0            0  
251            0            1            0            0  
252            0            0            0            0  

[253 rows x 18 columns]
In [188]:
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, 
                                                    random_state = 1)

print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))

print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
Train data shape of X = (202, 18) and y = (202,) : 
Test data shape of X = (51, 18) and y = (51,) : 

Lasso Regression:

In [189]:
import statsmodels.api as sm
from sklearn.linear_model import Lasso

# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)

# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)

# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")

# Fit regression model:
model = sm.OLS(y, X).fit()

# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")

lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
 
print(lasso_coeff)
Adjusted R-squared: 0.9901357619968777 

               Columns  Coefficient Estimate
0                  Arm              0.000000
1          QOLmental_0              0.023037
2        QOLphysical_0              0.645860
3        Flourishing_0              0.000000
4         Loneliness_0              0.000000
5   HealthMotivation_0              0.016063
6        Relatedness_0              0.114791
7            Anxiety_0             -0.000000
8         Depression_0             -0.153461
9        TechComfort_0              0.031195
10        TechPhyLim_0             -0.000000
11         WellBeing_0              0.000000
12             partner              0.000000
13          live_alone              0.000000
14         education_4              0.000000
15         education_5             -0.000000
16         education_6              0.000000
17         education_7              0.000000
In [190]:
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(70, 50))
 
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
 
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
 
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))

ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

plt.style.use('ggplot')

# Set general font size:
plt.rcParams['font.size'] = '100'

# Rotate x-axis tick labels:
plt.xticks(rotation = 90)

plt.show()

With "TechComfort_1" variable as RV:

In [191]:
from sklearn.linear_model import Lasso

# initiate lasso regression model:
model = Lasso()

# define predictor & response variables:

# define response variable:
y = df['TechComfort_1']

# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
       'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
       'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1']]
 # fit regression model:
model.fit(X, y)
Out[191]:
Lasso(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=1000,
      normalize=False, positive=False, precompute=False, random_state=None,
      selection='cyclic', tol=0.0001, warm_start=False)
In [192]:
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['TechComfort_1'])
data_target.shape
data['TechComfort_1'] = pd.Series(data_target)
data
Out[192]:
Arm QOLmental_0 QOLphysical_0 Flourishing_0 Loneliness_0 HealthMotivation_0 Relatedness_0 Anxiety_0 Depression_0 TechComfort_0 ... QOLphysical_3 Flourishing_3 Loneliness_3 HealthMotivation_3 Relatedness_3 Anxiety_3 Depression_3 TechComfort_3 TechPhyLim_3 WellBeing_3
0 1 51.6 42.0 36.0 17 16 32 4.0 4.0 11 ... 40.5 32 17 14 31.0 3 4 10 2 0
1 1 51.3 41.6 34.0 20 12 33 3.0 3.0 12 ... 41.4 30 21 16 30.0 5 4 12 0 1
2 0 54.8 50.4 36.0 8 16 39 0.0 2.0 15 ... 39.6 34 8 18 43.0 0 0 14 0 3
3 1 45.5 46.6 30.0 14 12 29 6.0 1.0 23 ... 42.5 27 19 12 28.0 0 1 28 0 0
4 0 46.6 51.5 40.0 8 18 36 2.0 2.0 6 ... 42.5 35 17 15 36.0 2 5 30 0 2
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
248 1 43.9 50.4 37.0 9 16 43 3.0 3.0 24 ... 50.4 39 10 16 45.0 2 2 21 0 0
249 0 50.9 44.0 33.0 9 19 36 1.0 2.0 14 ... 38.1 31 9 16 33.0 0 2 14 0 0
250 0 45.5 29.7 27.0 19 10 34 2.0 6.0 16 ... 37.5 31 16 12 44.0 0 4 18 2 1
251 0 39.2 42.5 30.0 18 14 22 2.0 2.0 26 ... 40.5 32 16 15 26.0 3 4 5 0 0
252 1 44.7 36.9 33.0 17 9 32 5.0 2.0 2 ... 36.9 31 19 13 29.0 3 5 4 0 0

253 rows × 51 columns

In [193]:
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
       'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
       'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1']]
y = data['TechComfort_1']
print(X)
     Arm  QOLmental_0  QOLphysical_0  Flourishing_0  Loneliness_0  \
0      1         51.6           42.0           36.0            17   
1      1         51.3           41.6           34.0            20   
2      0         54.8           50.4           36.0             8   
3      1         45.5           46.6           30.0            14   
4      0         46.6           51.5           40.0             8   
..   ...          ...            ...            ...           ...   
248    1         43.9           50.4           37.0             9   
249    0         50.9           44.0           33.0             9   
250    0         45.5           29.7           27.0            19   
251    0         39.2           42.5           30.0            18   
252    1         44.7           36.9           33.0            17   

     HealthMotivation_0  Relatedness_0  Anxiety_0  Depression_0  \
0                    16             32        4.0           4.0   
1                    12             33        3.0           3.0   
2                    16             39        0.0           2.0   
3                    12             29        6.0           1.0   
4                    18             36        2.0           2.0   
..                  ...            ...        ...           ...   
248                  16             43        3.0           3.0   
249                  19             36        1.0           2.0   
250                  10             34        2.0           6.0   
251                  14             22        2.0           2.0   
252                   9             32        5.0           2.0   

     TechComfort_0  ...  QOLphysical_1  Flourishing_1  Loneliness_1  \
0               11  ...           37.5           36.0          16.0   
1               12  ...           39.6           33.0          17.0   
2               15  ...           44.0           32.0          19.0   
3               23  ...           42.5           28.0          18.0   
4                6  ...           58.6           39.0          12.0   
..             ...  ...            ...            ...           ...   
248             24  ...           53.3           40.0           8.0   
249             14  ...           39.6           31.0          11.0   
250             16  ...           37.5           29.0          20.0   
251             26  ...           41.6           36.0          18.0   
252              2  ...           39.6           30.0          22.0   

     HealthMotivation_1  Relatedness_1  Anxiety_1  Depression_1  \
0                  14.0           27.0        0.0           0.0   
1                  12.0           33.0        5.0           5.0   
2                  14.0           29.0        1.0           6.0   
3                  12.0           26.0        2.0           2.0   
4                  14.0           35.0        2.0           2.0   
..                  ...            ...        ...           ...   
248                18.0           45.0        0.0           0.0   
249                16.0           37.0        2.0           2.0   
250                13.0           31.0        0.0           4.0   
251                12.0           25.0        0.0           5.0   
252                12.0           37.0        3.0           6.0   

     TechComfort_1  TechPhyLim_1  WellBeing_1  
0             11.0           0.0          2.0  
1             14.0           0.0          1.0  
2             14.0           0.0          0.0  
3             20.0           0.0          0.0  
4             30.0           1.0          3.0  
..             ...           ...          ...  
248           26.0           0.0          1.0  
249           14.0           0.0          1.0  
250           20.0           0.0          1.0  
251           29.0           0.0          1.0  
252            5.0           0.0          1.0  

[253 rows x 29 columns]
In [194]:
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, 
                                                    random_state = 1)

print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))

print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
Train data shape of X = (202, 29) and y = (202,) : 
Test data shape of X = (51, 29) and y = (51,) : 

Lasso Regression:

In [195]:
import statsmodels.api as sm
from sklearn.linear_model import Lasso

# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)

# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)

# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")

# Fit regression model:
model = sm.OLS(y, X).fit()

# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")

lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
 
print(lasso_coeff)
Adjusted R-squared: 1.0 

               Columns  Coefficient Estimate
0                  Arm              0.000000
1          QOLmental_0              0.000000
2        QOLphysical_0              0.000000
3        Flourishing_0              0.000000
4         Loneliness_0             -0.000000
5   HealthMotivation_0              0.000000
6        Relatedness_0              0.000000
7            Anxiety_0              0.000000
8         Depression_0             -0.000000
9        TechComfort_0              0.000000
10        TechPhyLim_0             -0.000000
11         WellBeing_0              0.000000
12             partner              0.000000
13          live_alone              0.000000
14         education_4             -0.000000
15         education_5              0.000000
16         education_6              0.000000
17         education_7             -0.000000
18         QOLmental_1              0.000000
19       QOLphysical_1              0.000000
20       Flourishing_1              0.000000
21        Loneliness_1             -0.000000
22  HealthMotivation_1             -0.000000
23       Relatedness_1              0.000000
24           Anxiety_1             -0.000000
25        Depression_1              0.000000
26       TechComfort_1              0.983695
27        TechPhyLim_1             -0.000000
28         WellBeing_1             -0.000000
In [196]:
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(70, 50))
 
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
 
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
 
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))

ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

plt.style.use('ggplot')

# Set general font size:
plt.rcParams['font.size'] = '100'

# Rotate x-axis tick labels:
plt.xticks(rotation = 90)

plt.show()

With "Loneliness_1" variable as RV:

In [197]:
from sklearn.linear_model import Lasso

# initiate lasso regression model:
model = Lasso()

# define predictor & response variables:

# define response variable:
y = df['Loneliness_1']

# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7']]
 # fit regression model:
model.fit(X, y)
Out[197]:
Lasso(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=1000,
      normalize=False, positive=False, precompute=False, random_state=None,
      selection='cyclic', tol=0.0001, warm_start=False)
In [198]:
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['Loneliness_1'])
data_target.shape
data['Loneliness_1'] = pd.Series(data_target)
data
Out[198]:
Arm QOLmental_0 QOLphysical_0 Flourishing_0 Loneliness_0 HealthMotivation_0 Relatedness_0 Anxiety_0 Depression_0 TechComfort_0 ... QOLphysical_3 Flourishing_3 Loneliness_3 HealthMotivation_3 Relatedness_3 Anxiety_3 Depression_3 TechComfort_3 TechPhyLim_3 WellBeing_3
0 1 51.6 42.0 36.0 17 16 32 4.0 4.0 11 ... 40.5 32 17 14 31.0 3 4 10 2 0
1 1 51.3 41.6 34.0 20 12 33 3.0 3.0 12 ... 41.4 30 21 16 30.0 5 4 12 0 1
2 0 54.8 50.4 36.0 8 16 39 0.0 2.0 15 ... 39.6 34 8 18 43.0 0 0 14 0 3
3 1 45.5 46.6 30.0 14 12 29 6.0 1.0 23 ... 42.5 27 19 12 28.0 0 1 28 0 0
4 0 46.6 51.5 40.0 8 18 36 2.0 2.0 6 ... 42.5 35 17 15 36.0 2 5 30 0 2
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
248 1 43.9 50.4 37.0 9 16 43 3.0 3.0 24 ... 50.4 39 10 16 45.0 2 2 21 0 0
249 0 50.9 44.0 33.0 9 19 36 1.0 2.0 14 ... 38.1 31 9 16 33.0 0 2 14 0 0
250 0 45.5 29.7 27.0 19 10 34 2.0 6.0 16 ... 37.5 31 16 12 44.0 0 4 18 2 1
251 0 39.2 42.5 30.0 18 14 22 2.0 2.0 26 ... 40.5 32 16 15 26.0 3 4 5 0 0
252 1 44.7 36.9 33.0 17 9 32 5.0 2.0 2 ... 36.9 31 19 13 29.0 3 5 4 0 0

253 rows × 51 columns

In [199]:
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7']]
y = data['Loneliness_1']
print(X)
     Arm  QOLmental_0  QOLphysical_0  Flourishing_0  Loneliness_0  \
0      1         51.6           42.0           36.0            17   
1      1         51.3           41.6           34.0            20   
2      0         54.8           50.4           36.0             8   
3      1         45.5           46.6           30.0            14   
4      0         46.6           51.5           40.0             8   
..   ...          ...            ...            ...           ...   
248    1         43.9           50.4           37.0             9   
249    0         50.9           44.0           33.0             9   
250    0         45.5           29.7           27.0            19   
251    0         39.2           42.5           30.0            18   
252    1         44.7           36.9           33.0            17   

     HealthMotivation_0  Relatedness_0  Anxiety_0  Depression_0  \
0                    16             32        4.0           4.0   
1                    12             33        3.0           3.0   
2                    16             39        0.0           2.0   
3                    12             29        6.0           1.0   
4                    18             36        2.0           2.0   
..                  ...            ...        ...           ...   
248                  16             43        3.0           3.0   
249                  19             36        1.0           2.0   
250                  10             34        2.0           6.0   
251                  14             22        2.0           2.0   
252                   9             32        5.0           2.0   

     TechComfort_0  TechPhyLim_0  WellBeing_0  partner  live_alone  \
0               11             2            0        0           0   
1               12             0            0        1           1   
2               15             0            0        1           1   
3               23             0            0        1           1   
4                6             2            0        0           1   
..             ...           ...          ...      ...         ...   
248             24             0            0        1           1   
249             14             1            1        0           1   
250             16             1            1        0           0   
251             26             0            0        0           0   
252              2             0            0        0           0   

     education_4  education_5  education_6  education_7  
0              0            0            0            1  
1              0            0            0            1  
2              0            0            1            0  
3              0            1            0            0  
4              0            0            1            0  
..           ...          ...          ...          ...  
248            0            0            1            0  
249            0            0            0            0  
250            0            0            0            0  
251            0            1            0            0  
252            0            0            0            0  

[253 rows x 18 columns]
In [200]:
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, 
                                                    random_state = 1)

print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))

print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
Train data shape of X = (202, 18) and y = (202,) : 
Test data shape of X = (51, 18) and y = (51,) : 

Lasso Regression:

In [201]:
import statsmodels.api as sm
from sklearn.linear_model import Lasso

# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)

# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)

# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")

# Fit regression model:
model = sm.OLS(y, X).fit()

# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")

lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
 
print(lasso_coeff)
Adjusted R-squared: 0.943617323761952 

               Columns  Coefficient Estimate
0                  Arm             -0.000000
1          QOLmental_0             -0.074712
2        QOLphysical_0              0.000000
3        Flourishing_0             -0.000000
4         Loneliness_0              0.431952
5   HealthMotivation_0             -0.000000
6        Relatedness_0             -0.202151
7            Anxiety_0              0.000000
8         Depression_0              0.195586
9        TechComfort_0             -0.001827
10        TechPhyLim_0              0.000000
11         WellBeing_0             -0.000000
12             partner              0.000000
13          live_alone              0.000000
14         education_4             -0.000000
15         education_5             -0.000000
16         education_6             -0.000000
17         education_7              0.000000
In [202]:
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(70, 50))
 
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
 
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
 
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))

ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

plt.style.use('ggplot')

# Set general font size:
plt.rcParams['font.size'] = '100'

# Rotate x-axis tick labels:
plt.xticks(rotation = 90)

plt.show()

With "HealthMotivation_1" variable as RV:

In [221]:
from sklearn.linear_model import Lasso

# initiate lasso regression model:
model = Lasso()

# define predictor & response variables:

# define response variable:
y = df['HealthMotivation_1']

# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7']]
 # fit regression model:
model.fit(X, y)
Out[221]:
Lasso(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=1000,
      normalize=False, positive=False, precompute=False, random_state=None,
      selection='cyclic', tol=0.0001, warm_start=False)
In [222]:
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['HealthMotivation_1'])
data_target.shape
data['HealthMotivation_1'] = pd.Series(data_target)
data
Out[222]:
Arm QOLmental_0 QOLphysical_0 Flourishing_0 Loneliness_0 HealthMotivation_0 Relatedness_0 Anxiety_0 Depression_0 TechComfort_0 ... QOLphysical_3 Flourishing_3 Loneliness_3 HealthMotivation_3 Relatedness_3 Anxiety_3 Depression_3 TechComfort_3 TechPhyLim_3 WellBeing_3
0 1 51.6 42.0 36.0 17 16 32 4.0 4.0 11 ... 40.5 32 17 14 31.0 3 4 10 2 0
1 1 51.3 41.6 34.0 20 12 33 3.0 3.0 12 ... 41.4 30 21 16 30.0 5 4 12 0 1
2 0 54.8 50.4 36.0 8 16 39 0.0 2.0 15 ... 39.6 34 8 18 43.0 0 0 14 0 3
3 1 45.5 46.6 30.0 14 12 29 6.0 1.0 23 ... 42.5 27 19 12 28.0 0 1 28 0 0
4 0 46.6 51.5 40.0 8 18 36 2.0 2.0 6 ... 42.5 35 17 15 36.0 2 5 30 0 2
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
248 1 43.9 50.4 37.0 9 16 43 3.0 3.0 24 ... 50.4 39 10 16 45.0 2 2 21 0 0
249 0 50.9 44.0 33.0 9 19 36 1.0 2.0 14 ... 38.1 31 9 16 33.0 0 2 14 0 0
250 0 45.5 29.7 27.0 19 10 34 2.0 6.0 16 ... 37.5 31 16 12 44.0 0 4 18 2 1
251 0 39.2 42.5 30.0 18 14 22 2.0 2.0 26 ... 40.5 32 16 15 26.0 3 4 5 0 0
252 1 44.7 36.9 33.0 17 9 32 5.0 2.0 2 ... 36.9 31 19 13 29.0 3 5 4 0 0

253 rows × 51 columns

In [223]:
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7']]
y = data['HealthMotivation_1']
print(X)
     Arm  QOLmental_0  QOLphysical_0  Flourishing_0  Loneliness_0  \
0      1         51.6           42.0           36.0            17   
1      1         51.3           41.6           34.0            20   
2      0         54.8           50.4           36.0             8   
3      1         45.5           46.6           30.0            14   
4      0         46.6           51.5           40.0             8   
..   ...          ...            ...            ...           ...   
248    1         43.9           50.4           37.0             9   
249    0         50.9           44.0           33.0             9   
250    0         45.5           29.7           27.0            19   
251    0         39.2           42.5           30.0            18   
252    1         44.7           36.9           33.0            17   

     HealthMotivation_0  Relatedness_0  Anxiety_0  Depression_0  \
0                    16             32        4.0           4.0   
1                    12             33        3.0           3.0   
2                    16             39        0.0           2.0   
3                    12             29        6.0           1.0   
4                    18             36        2.0           2.0   
..                  ...            ...        ...           ...   
248                  16             43        3.0           3.0   
249                  19             36        1.0           2.0   
250                  10             34        2.0           6.0   
251                  14             22        2.0           2.0   
252                   9             32        5.0           2.0   

     TechComfort_0  TechPhyLim_0  WellBeing_0  partner  live_alone  \
0               11             2            0        0           0   
1               12             0            0        1           1   
2               15             0            0        1           1   
3               23             0            0        1           1   
4                6             2            0        0           1   
..             ...           ...          ...      ...         ...   
248             24             0            0        1           1   
249             14             1            1        0           1   
250             16             1            1        0           0   
251             26             0            0        0           0   
252              2             0            0        0           0   

     education_4  education_5  education_6  education_7  
0              0            0            0            1  
1              0            0            0            1  
2              0            0            1            0  
3              0            1            0            0  
4              0            0            1            0  
..           ...          ...          ...          ...  
248            0            0            1            0  
249            0            0            0            0  
250            0            0            0            0  
251            0            1            0            0  
252            0            0            0            0  

[253 rows x 18 columns]
In [224]:
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, 
                                                    random_state = 1)

print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))

print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
Train data shape of X = (202, 18) and y = (202,) : 
Test data shape of X = (51, 18) and y = (51,) : 

Lasso Regression:

In [225]:
import statsmodels.api as sm
from sklearn.linear_model import Lasso

# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)

# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)

# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")

# Fit regression model:
model = sm.OLS(y, X).fit()

# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")

lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
 
print(lasso_coeff)
Adjusted R-squared: 0.9722730236012449 

               Columns  Coefficient Estimate
0                  Arm              0.000000
1          QOLmental_0             -0.000000
2        QOLphysical_0             -0.000000
3        Flourishing_0             -0.000000
4         Loneliness_0             -0.030131
5   HealthMotivation_0              0.275629
6        Relatedness_0              0.000000
7            Anxiety_0              0.000000
8         Depression_0              0.000000
9        TechComfort_0             -0.019184
10        TechPhyLim_0              0.000000
11         WellBeing_0             -0.000000
12             partner              0.000000
13          live_alone              0.000000
14         education_4              0.000000
15         education_5             -0.000000
16         education_6              0.000000
17         education_7              0.000000
In [226]:
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(70, 50))
 
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
 
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
 
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))

ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

plt.style.use('ggplot')

# Set general font size:
plt.rcParams['font.size'] = '100'

# Rotate x-axis tick labels:
plt.xticks(rotation = 90)

plt.show()

With "Depression_1" variable as RV:

In [209]:
from sklearn.linear_model import Lasso

# initiate lasso regression model:
model = Lasso()

# define predictor & response variables:

# define response variable:
y = df['Depression_1']

# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7']]
 # fit regression model:
model.fit(X, y)
Out[209]:
Lasso(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=1000,
      normalize=False, positive=False, precompute=False, random_state=None,
      selection='cyclic', tol=0.0001, warm_start=False)
In [210]:
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['Depression_1'])
data_target.shape
data['Depression_1'] = pd.Series(data_target)
data
Out[210]:
Arm QOLmental_0 QOLphysical_0 Flourishing_0 Loneliness_0 HealthMotivation_0 Relatedness_0 Anxiety_0 Depression_0 TechComfort_0 ... QOLphysical_3 Flourishing_3 Loneliness_3 HealthMotivation_3 Relatedness_3 Anxiety_3 Depression_3 TechComfort_3 TechPhyLim_3 WellBeing_3
0 1 51.6 42.0 36.0 17 16 32 4.0 4.0 11 ... 40.5 32 17 14 31.0 3 4 10 2 0
1 1 51.3 41.6 34.0 20 12 33 3.0 3.0 12 ... 41.4 30 21 16 30.0 5 4 12 0 1
2 0 54.8 50.4 36.0 8 16 39 0.0 2.0 15 ... 39.6 34 8 18 43.0 0 0 14 0 3
3 1 45.5 46.6 30.0 14 12 29 6.0 1.0 23 ... 42.5 27 19 12 28.0 0 1 28 0 0
4 0 46.6 51.5 40.0 8 18 36 2.0 2.0 6 ... 42.5 35 17 15 36.0 2 5 30 0 2
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
248 1 43.9 50.4 37.0 9 16 43 3.0 3.0 24 ... 50.4 39 10 16 45.0 2 2 21 0 0
249 0 50.9 44.0 33.0 9 19 36 1.0 2.0 14 ... 38.1 31 9 16 33.0 0 2 14 0 0
250 0 45.5 29.7 27.0 19 10 34 2.0 6.0 16 ... 37.5 31 16 12 44.0 0 4 18 2 1
251 0 39.2 42.5 30.0 18 14 22 2.0 2.0 26 ... 40.5 32 16 15 26.0 3 4 5 0 0
252 1 44.7 36.9 33.0 17 9 32 5.0 2.0 2 ... 36.9 31 19 13 29.0 3 5 4 0 0

253 rows × 51 columns

In [211]:
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7']]
y = data['Depression_1']
print(X)
     Arm  QOLmental_0  QOLphysical_0  Flourishing_0  Loneliness_0  \
0      1         51.6           42.0           36.0            17   
1      1         51.3           41.6           34.0            20   
2      0         54.8           50.4           36.0             8   
3      1         45.5           46.6           30.0            14   
4      0         46.6           51.5           40.0             8   
..   ...          ...            ...            ...           ...   
248    1         43.9           50.4           37.0             9   
249    0         50.9           44.0           33.0             9   
250    0         45.5           29.7           27.0            19   
251    0         39.2           42.5           30.0            18   
252    1         44.7           36.9           33.0            17   

     HealthMotivation_0  Relatedness_0  Anxiety_0  Depression_0  \
0                    16             32        4.0           4.0   
1                    12             33        3.0           3.0   
2                    16             39        0.0           2.0   
3                    12             29        6.0           1.0   
4                    18             36        2.0           2.0   
..                  ...            ...        ...           ...   
248                  16             43        3.0           3.0   
249                  19             36        1.0           2.0   
250                  10             34        2.0           6.0   
251                  14             22        2.0           2.0   
252                   9             32        5.0           2.0   

     TechComfort_0  TechPhyLim_0  WellBeing_0  partner  live_alone  \
0               11             2            0        0           0   
1               12             0            0        1           1   
2               15             0            0        1           1   
3               23             0            0        1           1   
4                6             2            0        0           1   
..             ...           ...          ...      ...         ...   
248             24             0            0        1           1   
249             14             1            1        0           1   
250             16             1            1        0           0   
251             26             0            0        0           0   
252              2             0            0        0           0   

     education_4  education_5  education_6  education_7  
0              0            0            0            1  
1              0            0            0            1  
2              0            0            1            0  
3              0            1            0            0  
4              0            0            1            0  
..           ...          ...          ...          ...  
248            0            0            1            0  
249            0            0            0            0  
250            0            0            0            0  
251            0            1            0            0  
252            0            0            0            0  

[253 rows x 18 columns]
In [212]:
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, 
                                                    random_state = 1)

print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))

print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
Train data shape of X = (202, 18) and y = (202,) : 
Test data shape of X = (51, 18) and y = (51,) : 

Lasso Regression:

In [213]:
import statsmodels.api as sm
from sklearn.linear_model import Lasso

# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)

# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)

# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")

# Fit regression model:
model = sm.OLS(y, X).fit()

# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")

lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
 
print(lasso_coeff)
Adjusted R-squared: 0.8266748673334949 

               Columns  Coefficient Estimate
0                  Arm             -0.000000
1          QOLmental_0             -0.063663
2        QOLphysical_0             -0.000433
3        Flourishing_0             -0.000000
4         Loneliness_0              0.005173
5   HealthMotivation_0             -0.000197
6        Relatedness_0             -0.029381
7            Anxiety_0              0.000000
8         Depression_0              0.675279
9        TechComfort_0              0.000000
10        TechPhyLim_0              0.000000
11         WellBeing_0             -0.000000
12             partner              0.000000
13          live_alone              0.000000
14         education_4             -0.000000
15         education_5              0.000000
16         education_6              0.000000
17         education_7              0.000000
In [214]:
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(100, 50))
 
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
 
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
 
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))

ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

plt.style.use('ggplot')

# Set general font size:
plt.rcParams['font.size'] = '100'

# Rotate x-axis tick labels:
plt.xticks(rotation = 90)

plt.show()

With "Relatedness_1" variable as RV:

In [215]:
from sklearn.linear_model import Lasso

# initiate lasso regression model:
model = Lasso()

# define predictor & response variables:

# define response variable:
y = df['Relatedness_1']

# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7']]
 # fit regression model:
model.fit(X, y)
Out[215]:
Lasso(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=1000,
      normalize=False, positive=False, precompute=False, random_state=None,
      selection='cyclic', tol=0.0001, warm_start=False)
In [216]:
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['Relatedness_1'])
data_target.shape
data['Relatedness_1'] = pd.Series(data_target)
data
Out[216]:
Arm QOLmental_0 QOLphysical_0 Flourishing_0 Loneliness_0 HealthMotivation_0 Relatedness_0 Anxiety_0 Depression_0 TechComfort_0 ... QOLphysical_3 Flourishing_3 Loneliness_3 HealthMotivation_3 Relatedness_3 Anxiety_3 Depression_3 TechComfort_3 TechPhyLim_3 WellBeing_3
0 1 51.6 42.0 36.0 17 16 32 4.0 4.0 11 ... 40.5 32 17 14 31.0 3 4 10 2 0
1 1 51.3 41.6 34.0 20 12 33 3.0 3.0 12 ... 41.4 30 21 16 30.0 5 4 12 0 1
2 0 54.8 50.4 36.0 8 16 39 0.0 2.0 15 ... 39.6 34 8 18 43.0 0 0 14 0 3
3 1 45.5 46.6 30.0 14 12 29 6.0 1.0 23 ... 42.5 27 19 12 28.0 0 1 28 0 0
4 0 46.6 51.5 40.0 8 18 36 2.0 2.0 6 ... 42.5 35 17 15 36.0 2 5 30 0 2
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
248 1 43.9 50.4 37.0 9 16 43 3.0 3.0 24 ... 50.4 39 10 16 45.0 2 2 21 0 0
249 0 50.9 44.0 33.0 9 19 36 1.0 2.0 14 ... 38.1 31 9 16 33.0 0 2 14 0 0
250 0 45.5 29.7 27.0 19 10 34 2.0 6.0 16 ... 37.5 31 16 12 44.0 0 4 18 2 1
251 0 39.2 42.5 30.0 18 14 22 2.0 2.0 26 ... 40.5 32 16 15 26.0 3 4 5 0 0
252 1 44.7 36.9 33.0 17 9 32 5.0 2.0 2 ... 36.9 31 19 13 29.0 3 5 4 0 0

253 rows × 51 columns

In [217]:
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
       'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
       'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
       'live_alone', 'education_4', 'education_5', 'education_6',
       'education_7']]
y = data['Relatedness_1']
print(X)
     Arm  QOLmental_0  QOLphysical_0  Flourishing_0  Loneliness_0  \
0      1         51.6           42.0           36.0            17   
1      1         51.3           41.6           34.0            20   
2      0         54.8           50.4           36.0             8   
3      1         45.5           46.6           30.0            14   
4      0         46.6           51.5           40.0             8   
..   ...          ...            ...            ...           ...   
248    1         43.9           50.4           37.0             9   
249    0         50.9           44.0           33.0             9   
250    0         45.5           29.7           27.0            19   
251    0         39.2           42.5           30.0            18   
252    1         44.7           36.9           33.0            17   

     HealthMotivation_0  Relatedness_0  Anxiety_0  Depression_0  \
0                    16             32        4.0           4.0   
1                    12             33        3.0           3.0   
2                    16             39        0.0           2.0   
3                    12             29        6.0           1.0   
4                    18             36        2.0           2.0   
..                  ...            ...        ...           ...   
248                  16             43        3.0           3.0   
249                  19             36        1.0           2.0   
250                  10             34        2.0           6.0   
251                  14             22        2.0           2.0   
252                   9             32        5.0           2.0   

     TechComfort_0  TechPhyLim_0  WellBeing_0  partner  live_alone  \
0               11             2            0        0           0   
1               12             0            0        1           1   
2               15             0            0        1           1   
3               23             0            0        1           1   
4                6             2            0        0           1   
..             ...           ...          ...      ...         ...   
248             24             0            0        1           1   
249             14             1            1        0           1   
250             16             1            1        0           0   
251             26             0            0        0           0   
252              2             0            0        0           0   

     education_4  education_5  education_6  education_7  
0              0            0            0            1  
1              0            0            0            1  
2              0            0            1            0  
3              0            1            0            0  
4              0            0            1            0  
..           ...          ...          ...          ...  
248            0            0            1            0  
249            0            0            0            0  
250            0            0            0            0  
251            0            1            0            0  
252            0            0            0            0  

[253 rows x 18 columns]
In [218]:
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, 
                                                    random_state = 1)

print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))

print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
Train data shape of X = (202, 18) and y = (202,) : 
Test data shape of X = (51, 18) and y = (51,) : 

Lasso Regression:

In [219]:
import statsmodels.api as sm
from sklearn.linear_model import Lasso

# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)

# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)

# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")

# Fit regression model:
model = sm.OLS(y, X).fit()

# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")

lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
 
print(lasso_coeff)
Adjusted R-squared: 0.9804808542472465 

               Columns  Coefficient Estimate
0                  Arm              0.000000
1          QOLmental_0              0.001193
2        QOLphysical_0              0.000000
3        Flourishing_0              0.000000
4         Loneliness_0             -0.279395
5   HealthMotivation_0              0.065144
6        Relatedness_0              0.492982
7            Anxiety_0             -0.000000
8         Depression_0             -0.292176
9        TechComfort_0              0.055605
10        TechPhyLim_0              0.000000
11         WellBeing_0              0.000000
12             partner             -0.000000
13          live_alone              0.000000
14         education_4              0.000000
15         education_5              0.000000
16         education_6             -0.000000
17         education_7             -0.000000
In [220]:
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(100, 50))
 
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
 
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
 
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))

ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

plt.style.use('ggplot')

# Set general font size:
plt.rcParams['font.size'] = '100'

# Rotate x-axis tick labels:
plt.xticks(rotation = 90)

plt.show()